package com.jiaozi.base.utils.bean;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.jiaozi.base.utils.bean.enums.REnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;

import java.time.LocalDateTime;


/**
 * 管理平台公共返回参数
 * @param <T>
 */
@Data
@NoArgsConstructor
public class R<T>  {

    @Schema(description="状态码" )
    private String code;
    @Schema(description = "描述")
    private String message;
    @Schema(description = "错误描述")
    private String errorMessage;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Schema(description = "响应时间")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime time = LocalDateTime.now();
    @Schema(description  = "响应信息")
    private T data;

    @Schema(description  = "新TOKEN")
    private String token;

    public R(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public R(String code, String message, String errorMessage, T data) {
        this.code = code;
        this.message = message;
        this.errorMessage = errorMessage;
        this.data = data;
    }

    public static R ok() {
        return new R(REnum.SUCCESS.getCode(), REnum.SUCCESS.getMsg(),
                null);
    }

    public static <T> R<T> ok(@Nullable T data) {
        return new R(REnum.SUCCESS.getCode(), REnum.SUCCESS.getMsg(),
                data);
    }

    public static <T> R<T> ok(String message) {
        return new R(REnum.SUCCESS.getCode(), message,"");
    }
    public static <T> R<T> ok(String message, T data) {
        return new R(REnum.SUCCESS.getCode(), message, data);
    }







    public static R error() {
        return error("");
    }
    public static R error(String message) {
        if(StrUtil.isNotBlank(message)){
            return new R(REnum.ERROR.getCode(), REnum.ERROR.getMsg(),message, null);
        }else{
            return new R(REnum.ERROR.getCode(), REnum.ERROR.getMsg(),REnum.ERROR.getMsg(), null);
        }
    }
    public static R error(REnum rEnum) {
        return error(rEnum, "");
    }
    public static R error(REnum rEnum, String message) {
        if(StrUtil.isNotBlank(message)){
            return new R(rEnum.getCode(), rEnum.getMsg(),message,null);
        }else{
            return new R(rEnum.getCode(), rEnum.getMsg(),rEnum.getMsg(),null);
        }


    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
